package com.stfalcon.chatkit.sample.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.stfalcon.chatkit.sample.R;
import com.stfalcon.chatkit.sample.common.data.model.User;
import com.stfalcon.chatkit.sample.holder.AbstractHolder;
import com.stfalcon.chatkit.sample.holder.special.UserHolder;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * 用户列表装饰器
 */
public class UserAdapter extends RecyclerView.Adapter<AbstractHolder> {

    private Context context;

    private List<User> userList;


    public UserAdapter(Context context, List<User> userList)
    {
        this.context = context;

        this.userList = userList;
    }

    @NotNull
    @Override
    public AbstractHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {

        return new UserHolder(LayoutInflater.from(context).inflate(R.layout.activity_user_layout, parent, false));

    }

    @Override
    public void onBindViewHolder(@NonNull AbstractHolder holder, int position) {

        User userItem = this.userList.get(position);

        holder.getUserName().setText(userItem.getName());

        holder.setUserId(userItem.getId());

        holder.getUserHeader().setImageDrawable(ContextCompat.getDrawable(context.getApplicationContext(),R.drawable.applogobig)); /// 这一步必须要做,否则不会显示.

    }

    /**
     * Set dialogs list
     *
     * @param items dialogs list
     */
    public void setItems(List<User> items) {
        this.userList = items;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return userList.size();
    }

}
